/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ems.entity.EmsOrder;
import com.thinkgem.jeesite.modules.ems.entity.EmsOrderRefund;
import com.thinkgem.jeesite.modules.ems.service.EmsOrderRefundService;
import com.thinkgem.jeesite.modules.ems.service.EmsOrderService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 退单Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsOrderRefund")
public class EmsOrderRefundController extends BaseController {

	@Autowired
	private EmsOrderRefundService emsOrderRefundService;
	
	@Autowired
	private EmsOrderService emsOrderService;
	
	@ModelAttribute
	public EmsOrderRefund get(@RequestParam(required=false) String id) {
		EmsOrderRefund entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsOrderRefundService.get(id);
		}
		if (entity == null){
			entity = new EmsOrderRefund();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsOrderRefund:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsOrderRefund emsOrderRefund, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmsOrderRefund> page = emsOrderRefundService.findPage(new Page<EmsOrderRefund>(request, response), emsOrderRefund); 
		model.addAttribute("page", page);
		return "modules/ems/emsOrderRefundList";
	}

	@RequiresPermissions("ems:emsOrderRefund:view")
	@RequestMapping(value = "form")
	public String form(EmsOrderRefund emsOrderRefund, Model model) {
		model.addAttribute("emsOrderRefund", emsOrderRefund);
		return "modules/ems/emsOrderRefundForm";
	}

	@RequiresPermissions("ems:emsOrderRefund:edit")
	@RequestMapping(value = "save")
	public String save(EmsOrderRefund emsOrderRefund, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsOrderRefund)){
			return form(emsOrderRefund, model);
		}
		emsOrderRefundService.save(emsOrderRefund);
		addMessage(redirectAttributes, "保存退单成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsOrderRefund/?repage";
	}
	
	// add by zad 2018-02-26 退款操作时，根据订单生成退款订单
	@RequiresPermissions("ems:emsOrderRefund:edit")
	@RequestMapping(value = "createRefundOrder")
	public String createRefundOrder(String orderId, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, orderId)){
			EmsOrderRefund emsOrderRefund = new EmsOrderRefund();
			EmsOrder order = new EmsOrder();
			order.setId(orderId);
			emsOrderRefund.setOrder(order);
			return form(emsOrderRefund, model);
		}
		
		EmsOrder order = this.emsOrderService.get(orderId);
		EmsOrderRefund orderRefund = new EmsOrderRefund();
		if(null != order)
		{
			orderRefund.setOrder(order);
			orderRefund.setName("退款订单");
			orderRefund.setType(order.getType());
			orderRefund.setSumMoney(order.getSumMoney());
			orderRefund.setPayMoney(order.getPayMoney());
			Date now = new Date();
			orderRefund.setSuccessDate(now);
			String type = order.getType();
			// 是否后台订单，后台订单时，状态直接为已退款
			if(Constant.Dict.DICT_BOOLEAN_YES.equals(type))
			{
				orderRefund.setStatus(Constant.OrderRefund.EMS_ORDERREFUND_STATUS_SUCCESS);
				orderRefund.setRemarks("后台订单，用户已退款");
			}
			if(Constant.Dict.DICT_BOOLEAN_NO.equals(type))
			{
				orderRefund.setStatus(Constant.OrderRefund.EMS_ORDERREFUND_STATUS_TOREFUND);
				orderRefund.setRemarks("前台订单，用户待退款");
			}
			this.emsOrderRefundService.save(orderRefund);
			addMessage(redirectAttributes, "生成退单成功");
		}
		
		// addMessage(redirectAttributes, "生成退单成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsOrderRefund/?repage";
	}
	
	@RequiresPermissions("ems:emsOrderRefund:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsOrderRefund emsOrderRefund, RedirectAttributes redirectAttributes) {
		emsOrderRefundService.delete(emsOrderRefund);
		addMessage(redirectAttributes, "删除退单成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsOrderRefund/?repage";
	}
	
	@RequiresPermissions("ems:emsOrderRefund:edit")
	@RequestMapping(value = "refund")
	public String refund(EmsOrderRefund emsOrderRefund, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsOrderRefund)){
			return form(emsOrderRefund, model);
		}
		emsOrderRefundService.save(emsOrderRefund);
		addMessage(redirectAttributes, "保存退单成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsOrderRefund/?repage";
	}

}